package com.lsa.letcode.the73.setMatrixZeroes;

public class Solution {
    public void setZeroes(int[][] m) {
        if (m.length == 0){
        	return;
        }
        else if (m[0].length == 0){
        	return;
        }
        
        boolean isFirstRow0 = false;
        boolean isFirstCol0 = false;
        
        for (int r = 0; r < m.length; r++) {
			if (m[r][0] == 0) {
				isFirstCol0 = true;
				break;
			}
		}
        
        for (int c = 0; c < m[0].length; c++) {
        	if (m[0][c] == 0) {
        		isFirstRow0 = true;
				break;
			}
		}
        
        for (int r = 1; r < m.length; r++) {
			for (int c = 1; c < m[0].length; c++) {
				if (m[r][c] == 0) {
					m[0][c] = 0;
					m[r][0] = 0;
				}
			}
		}
        System.out.print("");
        for (int r = 1; r < m.length; r++) {
			for (int c = 1; c < m[0].length; c++) {
				if (m[r][0] == 0 || m[0][c] == 0) {
					m[r][c] = 0;
				}
			}
		}
        
        if (isFirstCol0) {
        	for (int r = 0; r < m.length; r++) {
        		m[r][0] = 0;
    		}
        }
        
        if (isFirstRow0) {
        	for (int c = 0; c < m[0].length; c++) {
        		m[0][c] = 0;
        	}
        }
    }
}